package com.gitee.huanminabc.fastboot.load.stage.poststage;

import com.gitee.huanminabc.jcommon.reflect.ReflectPath;
import com.gitee.huanminabc.jcommon.str.StringUtil;
import com.gitee.huanminabc.fastboot.annotation.Value;
import com.gitee.huanminabc.fastboot.context.AppContext;
import com.gitee.huanminabc.fastboot.load.config.AppConfigUtil;
import com.gitee.huanminabc.fastboot.load.hook.ConfigValueInjectionFinishLifecycleHook;
import com.gitee.huanminabc.nullchain.Null;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 配置注入
 *
 * @Author: huanmin
 * @Date: 2025/4/2 23:21
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@Slf4j
public class ConfigValueInjectionStage  implements AppLoadPostStage {

    @Override
    public void run()  {
        for (Map.Entry<Class<?>, Object> classObjectEntry : AppContext.containerClassAll.entrySet()) {
            Class<?> key = classObjectEntry.getKey();
            Object value = classObjectEntry.getValue();
            Field[] declaredFields = key.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                Value annotation = declaredField.getAnnotation(Value.class);
                if (annotation==null) {
                    continue;
                }
                String fieldPath = ReflectPath.getField(declaredField);
                String expression = annotation.value();
                if (StringUtil.isEmpty(expression)){
                    log.error("{}的@Value表达式不能为空",fieldPath);
                    System.exit(1);
                }
                if (expression.startsWith("${")){
                    Object configValue = AppConfigUtil.getConfigValue(expression);
                    if (Null.is(configValue)){
                        log.error("{}的@Value(\"{}\")表达式在配置中没有找到",fieldPath,expression);
                        System.exit(1);
                    }
                    if (declaredField.getType().isAssignableFrom(configValue.getClass())){
                        try {
                            declaredField.set(value,configValue);
                        } catch (IllegalAccessException e) {
                            log.error("",e);
                            System.exit(1);
                        }
                    }else {
                        log.error("{}的@Value(\"{}\")表达式类型不匹配,期望类型为{},实际类型为{}",fieldPath,expression,declaredField.getType().getName(),configValue.getClass().getName());
                        System.exit(1);
                    }
                }else if (expression.startsWith("#{")){
                    //占时不支持,以后会补充
                }else {
                    log.error("{}的表达式不合法,只能以${或#{开头",fieldPath);
                    System.exit(1);
                }

            }
        }
        log.debug("ConfigValueInjectionStage load success");
        //运行加载配置注入完成的钩子
        new ConfigValueInjectionFinishLifecycleHook().run();
        //唤醒容器初始化
        synchronized ( ContainerInitCallBackStage.lock){
            ContainerInitCallBackStage.configValueInjectionStageOk=true;
            ContainerInitCallBackStage.lock.notify();
        }
    }
}
